import store from '@/store'

// 根据设备id返回设备名称
export function handleByDeviceType(type) {
  const typeArr = store.state.device.device_type
  return returnTypeFont(typeArr, type)
}

// 根据报警类型id返回名称
export function handleAlarmType(type) {
  const typeArr = store.state.device.alarm_type
  return returnTypeFont(typeArr, type)
}

function returnTypeFont(typeArr, type) {
  for (const i in typeArr) {
    if (typeArr[i].id === type) {
      return typeArr[i].enumCvalue
    }
  }
}

// 验证是不是json
export function isJSON(str) {
  if (typeof str === 'string') {
    try {
      var obj = JSON.parse(str)
      if (typeof obj === 'object' && obj) {
        return true
      } else {
        return false
      }
    } catch (e) {
      return false
    }
  }
}

